<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>确认支付</title>
    <link rel="stylesheet" href="../css/aui.2.0.css">
    <link rel="stylesheet" href="../css/alert.css">
    <link rel="stylesheet" href="../css/confirm-pay.css">
</head>
<body style="background-color: #f0f0f0">
<header class="aui-bar aui-bar-nav">
    <a class="aui-pull-left aui-btn">
        <span class="aui-iconfont aui-icon-left"></span>
    </a>
    <div class="aui-title">确认订单</div>
</header>
<nav>
    <div class="con-money">
        需支付: <span><span id="order-sales"></span>元</span>
    </div>
    <div class="con-pay-method">
        <!--<div class="con-pay-wechat">
            <div class="con-pay-left">
                <img src="../img/wx_pay.png" alt="">
                <span>微信支付</span>
                <span>微信安全支付</span>
            </div>
            <input class="aui-radio con-pay-select"  type="radio" name="radio1" value="1">
        </div>-->
        <div class="con-pay-wechat">
            <div class="con-pay-left">
                <img src="../img/zfb.png" alt="">
                <span>支付宝支付</span>
                <span>支付宝安全支付</span>
            </div>
            <input class="aui-radio con-pay-select" checked type="radio" name="radio1" value="2">
        </div>
    </div>
    <div class="con-confirm-pay" id="confirm-pay">确认支付</div>
</nav>

<script src="../js/jquery-3.0.0.min.js"></script>
<script src="../js/config.js"></script>
<script src="../js/alert.js"></script>
<script src="../js/function.js"></script>
<script src="../js/common.js"></script>
<!--<script src="../js/confirm-pay.js"></script>-->
<script>


    var sales=locationSearcher('paySales');
    var seckillOrderId=locationSearcher('orderId');
    var addressId=locationSearcher('addressId');
    console.log(addressId);

    $('#order-sales').html(sales);



    var payMethod=$('input[name=radio1]:checked').val();
    console.log(payMethod);

    $('.con-pay-select').click(function () {

        //获取支付方式，1为微信，2为支付宝
        payMethod=$('input[name=radio1]:checked').val();
    });

    $('#confirm-pay').bind('click',submitOrder);

    function submitOrder() {
        var loading = dialog.loading();
        $.ajax({
            url:C.interface.seckillPayBegin,
            type:'get',
            dataType:'json',
            data:{
                orderId:seckillOrderId,
                token:C.token
            },
            complete:function () {
                loading.close();
            },
            success:function (response) {
                if (response.result === 'success'){

                    var canPay=response.canPay;
                    if (canPay !== true){
                        alert('订单已过期');
                        return false;
                    }else {
                        //订单未过期，跳转支付
                        realGoPay();

                    }
                }else if (response.result ==='login'){
                    alert('登录过期请重新登录');
                    againLogin();

                }else{
                    alert(response.errorMsg);
                }
            }
        });


    }


    function realGoPay() {
        var loading = dialog.loading();
        $.ajax({
            url:C.interface.seckillRealOrder,
            type:'get',
            dataType:'json',
            data:{
                token:C.token,
                secOrderId:seckillOrderId,
                addressId:addressId
            },
            complete:function () {
                loading.close();
            },
            success:function (response) {
                if (response.result === 'success'){

                    var orderId=response.data.orderId;

                    var payData={
                        postType:'goPay',
                        orderId:orderId,
                        token:C.token,
                        method:payMethod
                    };
                    console.log(payData);

                    var ua = navigator.userAgent.toLowerCase();
                    if (/iphone|ipad|ipod/.test(ua)) {

                        iosGoPay(payData);
                    } else {
                        androidGoPay(JSON.stringify(payData));
                    }
                }else {
                    alert(response.errorMsg);
                }
            }
        })
    }

    //android调用支付方法。
    function androidGoPay(param) {
        window.huifa.goPay(param);
    }

    function iosGoPay(param) {
        window.webkit.messageHandlers.goPay.postMessage(param);
    }
</script>

</body>
</html>